package me.zhyx.music.analysis;

import lombok.extern.slf4j.Slf4j;
import me.zhyx.music.init.TaskQueue;
import me.zhyx.music.model.Music;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @Auther: yssq
 * @Date: 2019/3/14 14:15
 * @Description:
 */
@Slf4j
public class HtmlAnalysis {
    public static void getLink(String html,String url){
        String uri=null;
        String password=null;

        try {
            Document parse = Jsoup.parse(html);
            Elements songName = parse.select("h2");
            Elements format = parse.getElementsByClass("cb");
            Elements singer = parse.getElementsByClass("signame");
            Elements downurl = parse.getElementsByClass("downurl");
            String size = parse.select("h2~p").next().text();
            String text = downurl.text();
            String replace = text.replace("链接:", "^").replace("提取码:", "&").replace("密码:", "&");
            String[] s = replace.split("&");
            if(s.length<1){
                return;
            }
            uri = s[0].substring(s[0].indexOf("^")+1).trim();
            password= s[1].trim();
            if(password.length()>4){
                TaskQueue.productException.add(url);
                return;
            }
            String name = songName.text().substring(0, songName.text().indexOf(" "));
            Music music = new Music(uri,password);
            music.setFormat(format.text());
            music.setSongName(name);
            music.setSinger(singer.text());
            music.setSourceAddress(url);
            music.setSize(size.substring(0,size.indexOf(" ")));
            TaskQueue.product.add(music);

        }catch (Exception e){
            e.printStackTrace();
            TaskQueue.productException.add(url);
        }

    }


}
